<template>
  <view>
    <cu-custom bgColor="bg-gradual-blue" :isBack="true">
      <block slot="backText">返回</block>
    </cu-custom>
    <view class="container">
      <!-- 标题 -->
      <view class="title">个人信息</view>

      <!-- 表单 -->
      <uni-forms ref="form" @submit="submitForm">
        <!-- 姓名 -->
        <uni-forms-item label="姓名" name="name">
          <uni-easyinput v-model="formData.name" placeholder="请输入姓名"></uni-easyinput>
        </uni-forms-item>

        <!-- 住院号 -->
        <uni-forms-item label="住院号" name="hospitalNo">
          <uni-easyinput v-model="formData.hospitalNo" placeholder="请输入住院号"></uni-easyinput>
        </uni-forms-item>

        <!-- 诊断 -->
        <uni-forms-item label="诊断" name="diagnosis">
          <uni-easyinput v-model="formData.diagnosis" placeholder="请输入诊断"></uni-easyinput>
        </uni-forms-item>

        <!-- 年龄 -->
        <uni-forms-item label="年龄" name="age">
          <uni-easyinput v-model="formData.age" placeholder="请输入年龄" type="number"></uni-easyinput>
        </uni-forms-item>

        <!-- 性别选择器 -->
        <uni-forms-item label="性别" name="gender">
          <uni-data-picker :localdata="genders" v-model="formData.gender"></uni-data-picker>
        </uni-forms-item>

        <!-- 生育史 -->
        <uni-forms-item label="生育史" name="fertilityHistory">
          <uni-easyinput v-model="formData.fertilityHistory" placeholder="请输入生育史"></uni-easyinput>
        </uni-forms-item>

        <!-- 入院时的各种评分（AI） -->
      <!--  <uni-forms-item label="入院时的各种评分（AI）" name="admissionScore">
          <uni-easyinput v-model="formData.admissionScore" placeholder="请输入入院时的各种评分（AI）"></uni-easyinput>
        </uni-forms-item> -->

        <!-- 出院时评分（AI） -->
     <!--   <uni-forms-item label="出院时评分（AI）" name="dischargeScore">
          <uni-easyinput v-model="formData.dischargeScore" placeholder="请输入出院时评分（AI）"></uni-easyinput>
        </uni-forms-item> -->

        <!-- 2个月时体温 -->
        <uni-forms-item label="2个月时体温" name="temperature2Months">
          <uni-easyinput v-model="formData.temperature2Months" placeholder="请输入2个月时体温" type="digit"></uni-easyinput>
        </uni-forms-item>

        <!-- 2个月时脉搏 -->
        <uni-forms-item label="2个月时脉搏" name="pulseRate2Months">
          <uni-easyinput v-model="formData.pulseRate2Months" placeholder="请输入2个月时脉搏" type="number"></uni-easyinput>
        </uni-forms-item>

        <!-- 2个月时呼吸频率 -->
        <uni-forms-item label="2个月时呼吸" name="respiratoryRate2Months">
          <uni-easyinput v-model="formData.respiratoryRate2Months" placeholder="请输入2个月时呼吸频率" type="number"></uni-easyinput>
        </uni-forms-item>

        <!-- 2个月时血压 -->
        <uni-forms-item label="2个月时血压" name="bloodPressure2Months">
          <uni-easyinput v-model="formData.bloodPressure2Months" placeholder="请输入2个月时血压"></uni-easyinput>
        </uni-forms-item>

        <!-- 2个月时评分（AI） -->
      <!--  <uni-forms-item label="2个月时评分（AI）" name="score2Months">
          <uni-easyinput v-model="formData.score2Months" placeholder="请输入2个月时评分（AI）"></uni-easyinput>
        </uni-forms-item> -->

        <!-- 婚姻状况选择器 -->
        <uni-forms-item label="婚姻" name="maritalStatus">
          <uni-data-picker :localdata="maritalStatuses" v-model="formData.maritalStatus"></uni-data-picker>
        </uni-forms-item>

        <!-- 受教育程度选择器 -->
        <uni-forms-item label="受教育程度" name="educationLevel">
          <uni-data-picker :localdata="educationLevels" v-model="formData.educationLevel"></uni-data-picker>
        </uni-forms-item>

        <!-- 职业选择器 -->
        <uni-forms-item label="职业" name="occupation">
          <uni-data-picker :localdata="occupations" v-model="formData.occupation"></uni-data-picker>
        </uni-forms-item>

        <!-- 医疗费用选择器 -->
        <uni-forms-item label="医疗费用承担方式" name="medicalCost">
          <uni-data-picker :localdata="medicalCosts" v-model="formData.medicalCost"></uni-data-picker>
        </uni-forms-item>

        <!-- 居住地选择器 -->
        <uni-forms-item label="居住地" name="address">
          <uni-data-picker :localdata="addresss" v-model="formData.address"></uni-data-picker>
        </uni-forms-item>

        <!-- 电话 -->
        <uni-forms-item label="电话" name="phone">
          <uni-easyinput v-model="formData.phone" placeholder="请输入电话"></uni-easyinput>
        </uni-forms-item>

        <!-- 提交按钮 -->
        <view class="button-group">
          <button type="primary" @click="submitForm">提交</button>
        </view>
      </uni-forms>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userinfo: {},
      userid: "",
      formData: {
        name: '',
        hospitalNo: '',
        diagnosis: '',
        age: '',
        gender: '',
        fertilityHistory: '',
        // admissionScore: '',
        // dischargeScore: '',
        temperature2Months: '',
        pulseRate2Months: '',
        respiratoryRate2Months: '',
        bloodPressure2Months: '',
        // score2Months: '',
        maritalStatus: '',
        educationLevel: '',
        occupation: '',
        medicalCost: '',
        address: '',
        phone: ''
      },
      genders: [
        { text: '男', value: 'MALE' },
        { text: '女', value: 'FEMALE' }
      ],
      maritalStatuses: [
        { text: '未婚', value: 'single' },
        { text: '已婚', value: 'married' },
        { text: '丧偶', value: 'widowed' },
        { text: '离婚', value: 'divorced' },
        { text: '未说明的婚姻状况', value: 'unspecified' }
      ],
      educationLevels: [
        { text: '文盲及半文盲', value: 'illiterate' },
        { text: '小学', value: 'primary' },
        { text: '初中', value: 'junior' },
        { text: '高中/技校/中专', value: 'senior' },
        { text: '大专', value: 'college' },
        { text: '本科', value: 'bachelor' },
        { text: '硕士及以上', value: 'master' }
      ],
      occupations: [
        { text: '公务员', value: 'civil_servant' },
        { text: '企事业单位人员', value: 'enterprise_staff' },
        { text: '教师', value: 'teacher' },
        { text: '医务人员', value: 'medical_staff' },
        { text: '科研人员', value: 'researcher' },
        { text: '其他专业技术人员', value: 'other_professionals' },
        { text: '商业、服务业人员', value: 'service_staff' },
        { text: '农民', value: 'farmer' },
        { text: '工人', value: 'worker' },
        { text: '军人', value: 'military' },
        { text: '其他', value: 'other' }
      ],
      medicalCosts: [
        { text: '城镇职工基本医疗保险', value: 'basic_medical_insurance' },
        { text: '城镇居民基本医疗保险', value: 'resident_medical_insurance' },
        { text: '受新型农村合作医疗', value: 'new_rural_cooperative_medical_system' },
        { text: '贫困救助', value: 'poverty_alleviation' },
        { text: '商业医疗保险', value: 'commercial_insurance' },
        { text: '全公费', value: 'full_public_expense' },
        { text: '全自费', value: 'full_self_expense' },
        { text: '其他', value: 'other' }
      ],
      addresss: [
        { text: '城市', value: 'city' },
        { text: '农村', value: 'rural' }
      ]
    };
  },
  mounted() {
    this.userinfo = uni.getStorageSync("userinfo");
    this.userid = this.userinfo["data"]["userinfo"][0];
    console.log("用户信息", this.userid);
    this.fillFormData(this.userinfo["data"]["userinfo"]);
  },
  methods: {
    fillFormData(a) {
      this.formData.name = a[1];
      this.formData.hospitalNo = a[2];
      this.formData.diagnosis = a[4];
      this.formData.age = a[6];
      this.formData.gender = a[8];
      this.formData.fertilityHistory = a[9];
      // this.formData.admissionScore = a[24];
      // this.formData.dischargeScore = a[25];
      this.formData.temperature2Months = a[20];
      this.formData.pulseRate2Months = a[21];
      this.formData.respiratoryRate2Months = a[22];
      this.formData.bloodPressure2Months = a[23];
      // this.formData.score2Months = a[30];
      this.formData.maritalStatus = a[24];
      this.formData.educationLevel = a[25];
      this.formData.occupation = a[26];
      this.formData.medicalCost = a[27];
      this.formData.address = a[28];
      this.formData.phone = a[29];
    },
    submitForm() {
      const requestData = {
        name: this.formData.name,
        admission_no: this.formData.hospitalNo,
        diagnosis: this.formData.diagnosis,
        age: parseInt(this.formData.age, 10),
        gender: this.formData.gender,
        fertility_history: this.formData.fertilityHistory,
        // admission_score: this.formData.admissionScore,
        // discharge_score: this.formData.dischargeScore,
        temperature_2_months: parseFloat(this.formData.temperature2Months),
        pulse_2_months: parseInt(this.formData.pulseRate2Months, 10),
        respiration_rate_2_months: parseInt(this.formData.respiratoryRate2Months, 10),
        blood_pressure_2_months: this.formData.bloodPressure2Months,
        // score_2_months: this.formData.score2Months,
        marital_status: this.formData.maritalStatus,
        education_level: this.formData.educationLevel,
        occupation: this.formData.occupation,
        methods_of_medical_cost_coverage: this.formData.medicalCost,
        address: this.formData.address,
        phone: this.formData.phone
      };

      uni.request({
        url: `http://202.127.200.31:30010/user/users/${this.userid}`,
        method: 'PUT',
        data: requestData,
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          console.log('提交成功:', res.data);
          uni.showToast({
            title: '即将重新登陆.',
            icon: 'success',
            duration: 3000
          });
          setTimeout(() => {
            uni.navigateTo({
              url: '../me/login'
            });
          }, 1000);
        },
        fail: (err) => {
          console.error('提交失败:', err);
          uni.showToast({
            title: '提交失败',
            icon: 'none',
            duration: 2000
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
  background: linear-gradient(135deg, #0099cc, #00cc99, #66ccff);
  min-height: 100vh;
  font-family: 'Arial', sans-serif;
}

.title {
  font-size: 28px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 30px;
  color: #fff;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.uni-forms-item {
  margin-bottom: 20px;
  background: rgba(255, 255, 255, 0.9);
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.uni-forms-item label {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
  display: block;
}

.uni-easyinput {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
}

.button-group {
  text-align: center;
  margin-top: 30px;
}

button {
  padding: 12px 24px;
  font-size: 18px;
  color: #fff;
  background: linear-gradient(135deg, #0099cc, #00cc99);
  border: none;
  border-radius: 25px;
  cursor: pointer;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  transition: background 0.3s, transform 0.3s;
}

button:hover {
  background: linear-gradient(135deg, #00cc99, #0099cc);
  transform: translateY(-2px);
}

button:active {
  transform: translateY(1px);
}
</style>